package com.example.myapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceView;

import com.sanstar.baiduFacePay.BaiduFacePay;
import com.sanstar.baiduFacePay.callback.IBaiduFaceCB;

import java.lang.ref.WeakReference;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private final static String TAG = "MainActivity";

    MyHandler mHandler;

    HashMap<String, Object> map = new HashMap<>();
    SurfaceView surface;

    private class MyHandler extends Handler {

        private WeakReference<Activity> activityWeakReference;

        public MyHandler(Activity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity activity = activityWeakReference.get();
            Log.d(TAG, "msg = " + msg.what);
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        BaiduFacePay.getInstance().preloadSdkEnv(map, new IBaiduFaceCB() {
                            @Override
                            public void response(Map info) {
                                String msg = (String) info.get("return_msg");
                                int code = (int) info.get("return_code");
                                Log.d(TAG, "preloadSdkEnv msg = " + msg + ", code = " + code);
                                if(msg.equals("SUCC")) {
                                    mHandler.sendEmptyMessage(2);
                                    runOnUiThread(() -> startLoadImage());
                                }
                            }
                        });
                        break;
                    case 2:
                        BaiduFacePay.getInstance().manualUpdateFaceData(map, new IBaiduFaceCB() {
                            @Override
                            public void response(Map map) {
                                String msg = (String) map.get("return_msg");
                                int code = (int) map.get("return_code");
                                Log.d(TAG, "manualUpdateFaceData msg = " + msg + ", code = " + code);
                                if(msg.equals("SUCC")) {
                                    mHandler.sendEmptyMessage(3);
                                }
                            }
                        });
                        break;
                    case 3:
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, CameraTestActivity.class);
                        startActivity(intent);


                        //Intent intent = new Intent();
                        //intent.setClass(MainActivity.this, AddFaceActivity.class);
                        //startActivity(intent);

                        //获取特征值
                      //  startLoadImage();

                        break;
                    default:
                        break;
                }
            }
        }
    }


    private void startLoadImage() {
        HashMap<String, Object> map = new HashMap<>();
     //   map.put("pictureURL", "/sdcard/tiantianqin.jpg");
        map.put("pictureURL", "/sdcard/345.png");
        BaiduFacePay.getInstance().getFaceFeature(map, new IBaiduFaceCB() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void response(Map map) {
                String msg = (String) map.get("return_msg");
                int code = (int) map.get("return_code");
                byte[] feature = (byte[]) map.get("feature");
                Log.d(TAG, "get feature msg = " + msg + ", code = " + code);
                Log.d(TAG, "feature = " + Base64.getEncoder().encodeToString(feature));
            }
        });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHandler = new MyHandler(this);

        BaiduFacePay.getInstance().initBaiduFace(MyAPP.getInstance(), map, new IBaiduFaceCB() {

            @Override
            public void response(Map info) {
                if (info == null) {
                    new RuntimeException("调用返回为空").printStackTrace();
                    return ;
                }
                String msg = (String) info.get("return_msg");
                int code = (int) info.get("return_code");
                if(msg == null || !msg.equals("SUCC")) {
                    new RuntimeException("调用返回非成功信息：" + msg).printStackTrace();
                }
                Log.d("app", "init msg = " + msg + ", code = " + code);
                mHandler.sendEmptyMessage(1);
            }
        });

        findViewById(R.id.face_test).setOnClickListener((view) -> {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, CameraTestActivity.class);
            startActivity(intent);
        });

    }
}